import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

public class TextGraphProcessorTest2 {

    private TextGraphProcessor processor;

    @Before
    public void setUp() throws Exception {
        processor = new TextGraphProcessor("Easy_Test.txt");
        processor.readTextAndBuildGraph();
    }

    // 起点和终点之间存在路径（中间路径）
    @Test
    public void testCalcShortestPath_DirectPath() {
        String result = processor.calcShortestPath("scientist", "team");
        assertTrue(result.contains("scientist -> analyzed -> the -> team"));
    }

    // 起点和终点相同
    @Test
    public void testCalcShortestPath_SameWord() {
        String result = processor.calcShortestPath("scientist", "scientist");
        assertEquals("The shortest path from \"scientist\" to \"scientist\" is: scientist (距离: 0)", result);
    }

    // 起点存在但终点不存在
    @Test
    public void testCalcShortestPath_NoPath() {
        String result = processor.calcShortestPath("scientist", "nonexistent");
        assertEquals("No path from \"scientist\" to \"nonexistent\"!", result);
    }

    // 较长路径存在
    @Test
    public void testCalcShortestPath_LongerPath() {
        String result = processor.calcShortestPath("data", "again");
        assertTrue(result.contains("data ->"));
        assertTrue(result.contains("again"));
    }

    // 起点不存在
    @Test
    public void testCalcShortestPath_WordNotInGraph() {
        String result = processor.calcShortestPath("nonexistent", "team");
        assertEquals("No nonexistent or team in the graph!", result);
    }

    // 空字符串作为输入
    @Test
    public void testCalcShortestPath_EmptyInput() {
        String result = processor.calcShortestPath("", "team");
        assertEquals("No  or team in the graph!", result);
    }

    // 图中断开的节点（虽然两个词都存在但没有连通路径）
    @Test
    public void testCalcShortestPath_DisconnectedWords() {
        // 确保 "again" 和 "team" 没有路径
        String result = processor.calcShortestPath("again", "team");
        if (!result.startsWith("The shortest path")) {
            assertEquals("No path from \"again\" to \"team\"!", result);
        } else {
            fail("这两个词应当没有路径");
        }
    }

    // 图完全构建前尝试查询
    @Test
    public void testCalcShortestPath_UnbuiltGraph() {
        TextGraphProcessor emptyProcessor = new TextGraphProcessor("Easy_Test.txt");
        String result = emptyProcessor.calcShortestPath("scientist", "team");
        assertEquals("No scientist or team in the graph!", result);
    }
}
